package csnet.openflow.config;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import csnet.openflow.component.packetGenerator.PcapGenerator;
import csnet.openflow.packet.filter.HeaderFilter;
import csnet.openflow.packet.filter.PacketFilter;

public class PcapGeneratorInterpreter {
    private final static String FILTER = "filter";
    private final static String PCAP_PATH = "pcap_path";
    private final static String NETWORK = "network";
    private final static String TRANSPORT = "transport";

    public static PcapGenerator interpreter(JSONObject json)
            throws JSONException {
        PacketFilter filter = getFilter(json.getJSONObject(FILTER));
        PcapGenerator generator = new PcapGenerator(filter);
        JSONArray pathes = json.getJSONArray(PCAP_PATH);
        for (int i = pathes.length(); i > 0; --i) {
            generator.addPcap(pathes.getString(i - 1));
        }
        return generator;
    }

    private static PacketFilter getFilter(JSONObject json) throws JSONException {
        HeaderFilter filter = new HeaderFilter();
        JSONArray networkIds = json.getJSONArray(NETWORK);
        for (int i = networkIds.length(); i > 0; --i) {
            filter.addPermitedNetworkId(networkIds.getString(i - 1));
        }
        JSONArray transportIds = json.getJSONArray(TRANSPORT);
        for (int i = transportIds.length(); i > 0; --i) {
            filter.addPermitedTransportId(transportIds.getString(i - 1));
        }
        return filter;
    }

    public static void main(String args[]) throws JSONException {
        String json = "{\"filter\":{\"network\":[\"ipv4\"],\"transport\":[\"tcp\",\"udp\"]},\"pcap_path\":[\"test\"]}";
        System.out.println(interpreter(new JSONObject(json)));
    }
}
